package com.nuanshui.heatedloan.credit.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.nuanshui.heatedloan.constant.RabbitMqQueue;
import com.nuanshui.heatedloan.credit.facade.ApplyOrderService;
import com.nuanshui.heatedloan.credit.response.ApplyLoanResponse;
import com.nuanshui.heatedloan.middleware.rabbitmq.annotation.MqMessageHandler;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by wangxw on 2017/8/2.
 */

@MqMessageHandler(RabbitMqQueue.RABBIT_MQ_PAY_ORDER)
public class PayOrderHandler {

    private static Logger logger = LoggerFactory.getLogger("PayOrderHandler");


    @Autowired
    private ApplyOrderService applyOrderService;

    public void onMessage(Message message, Channel channel ) throws Exception{
        try{
            logger.info("=====开始执行支付订单消息队列======");
            String msg = new String(message.getBody());
            ApplyLoanResponse applyLoanResponse= JSON.parseObject(msg,ApplyLoanResponse.class);
            applyOrderService.playMoney(applyLoanResponse);
            logger.info("=====完成执行支付订单消息队列======");
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }
    }

}
